package com.pay.h5.controller;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.pay.constant.Constant;
import com.pay.entity.Merchant;
import com.pay.entity.MerchantAccount;
import com.pay.entity.Order;
import com.pay.entity.ZfbMember;
import com.pay.h5.mapping.MappingUtils;
import com.pay.service.IBaseInfoService;
import com.pay.service.IMerchantAccountService;
import com.pay.service.IMerchantService;
import com.pay.service.IOrderService;
import com.pay.service.IZfbMemberService;
import com.pay.utils.FunctionUtils;

import cn.hutool.core.text.UnicodeUtil;
import cn.hutool.core.util.StrUtil;

@Controller
public class H5Controller {
	
	@Autowired
	private IOrderService orderService ;
	
	@Autowired
	private IBaseInfoService baseInfoService ;
	
	@Autowired
	private IZfbMemberService zfbMemberService ;
	
	@Autowired
	private IMerchantAccountService merchantAccountService ;
	
	@Autowired
	private IMerchantService merchantService ;
	
	@RequestMapping(value=MappingUtils.toPay + "/{platno}")
	public ModelAndView toPay(@PathVariable String platno) throws UnsupportedEncodingException {
		Order order = orderService.getByPlatno(platno) ;
		if(order == null) {
			return messagePage("该订单已关闭") ;
		}
		if(order.getPaystatus() == Constant.pay_success) {
			return messagePage("订单已支付") ;
		}
		if(order.getPaystatus() == Constant.pay_close) {
			return messagePage("订单已关闭") ;
		}
		MerchantAccount account = merchantAccountService.getByMerId(order.getMerchantId());
		if(account == null) {
			return messagePage("商户通道已关闭") ;
		}
		if(account.getMoney().compareTo(BigDecimal.ZERO) < 0) {
			return messagePage("商户通道已关闭") ;
		}
		Merchant merchant = merchantService.getById(account.getMerId());
		ZfbMember zfbMember = null ;
		if(merchant.getModel() == Constant.merchant_to_public) {
			//对公
			zfbMember = zfbMemberService.getRandOnline();
		}
		if(merchant.getModel() == Constant.merchant_to_private) {
			//对私
			zfbMember = zfbMemberService.getPrivateRobto(merchant.getZfbMemberId()) ; 
		}
		if(zfbMember == null) {
			return messagePage("通道已关闭") ;
		}
		
		
		
		String h5Domain = baseInfoService.getByKey(Constant.h5Domain);
		String url = h5Domain + MappingUtils.handlePay + "/" + platno ;
		BigDecimal money = FunctionUtils.div(new BigDecimal(order.getAmount()), new BigDecimal(100), 2);
		ModelAndView m = new ModelAndView(MappingUtils.toPay);
		m.addObject("url", url) ;
		m.addObject("order", order) ;
		m.addObject("zfbMember", zfbMember) ;
		m.addObject("money", money) ;
		//m.setViewName("redirect:alipays://platformapi/startapp?appId=20000123&actionType=scan&biz_data=" + URLEncoder.encode("{\"s\": \"money\",\"u\": \"" + zfbMember.getUserId() + "\",\"a\": \"" + money + "\",\"m\": \"b=" + order.getFlagcode() + "\"}", "UTF-8"));
		
		return m ;
	}
	
	/**
	 * 这边由支付宝来调用的 做商家收款码使用 大部分手机不支持
	 * @param platno
	 * @return
	 */
	@RequestMapping(value=MappingUtils.handlePay + "/{platno}")
	public ModelAndView handlePay(@PathVariable String platno,HttpServletRequest request) {
		String userAgent = request.getHeader("User-Agent");
		if(StrUtil.isBlank(userAgent)) {
			return new ModelAndView("redirect:http://www.baidu.com") ;
		}
		if(userAgent.indexOf("AlipayClient") == -1) {
			return new ModelAndView("redirect:http://www.baidu.com") ;
		}
		Order order = orderService.getByPlatno(platno) ;
		if(order == null) {
			return messagePage("该订单已关闭") ;
		}
		if(order.getPaystatus() == Constant.pay_success) {
			return messagePage("订单已支付") ;
		}
		if(order.getPaystatus() == Constant.pay_close) {
			return messagePage("订单已关闭") ;
		}
		MerchantAccount account = merchantAccountService.getByMerId(order.getMerchantId());
		if(account == null) {
			return messagePage("商户通道已关闭") ;
		}
		if(account.getMoney().compareTo(BigDecimal.ZERO) < 0) {
			return messagePage("商户通道已关闭") ;
		}
		Merchant merchant = merchantService.getById(account.getMerId());
		ZfbMember zfbMember = null ;
		if(merchant.getModel() == Constant.merchant_to_public) {
			//对公
			zfbMember = zfbMemberService.getRandOnline();
		}
		if(merchant.getModel() == Constant.merchant_to_private) {
			//对私
			zfbMember = zfbMemberService.getPrivateRobto(merchant.getZfbMemberId()) ; 
		}
		if(zfbMember == null) {
			return messagePage("通道已关闭") ;
		}
		
		order.setFlagcode(UnicodeUtil.toUnicode(order.getFlagcode()));
		BigDecimal money = FunctionUtils.div(new BigDecimal(order.getAmount()), new BigDecimal(100), 2);
		String h5Domain = baseInfoService.getByKey(Constant.h5Domain);
		String url = h5Domain + MappingUtils.handlePay + "/" + platno ;
		ModelAndView m = new ModelAndView(MappingUtils.handlePay);
		m.addObject("order", order) ;
		m.addObject("zfbMember", zfbMember) ;
		m.addObject("money", money) ;
		m.addObject("url", url) ;
		return m ;
	}
	
	
	@RequestMapping(value=MappingUtils.messagePage)
	public ModelAndView messagePage(String message) {
		ModelAndView m = new ModelAndView(MappingUtils.messagePage);
		m.addObject("message", message) ;
		return m ; 
	}
}
